window.functions = window.functions || {};

let PopupViewBase = require('./PopupViewBase');
//弹出视图包装
const POPUP_VIEW_PREFAB = "prefabs/ui/PopupViewRoot";   //PopupView的prefab动态加载路径

//已弹出窗口数量
let popUpViewCount = 0;

let PopupViewRoot = cc.Class({
  extends: cc.Component,
  properties: {
    blackMarkNode: cc.Node,
    contentParent: cc.Node,
    anim: cc.Animation,
    popupAudioClip: {
      type: cc.AudioClip,
      default: null
    },
    closeAudioClip: {
      type: cc.AudioClip,
      default: null
    }
  },
  ctor() {
    this.data = null;
    this.contentNode = null;
    this.opts = null;
  },
  
  onLoad() {
  
  },
  
  setContent(contentNode, data, opts) {
    let popupView = contentNode.getComponent(PopupViewBase);
    if (!popupView) {
      console.error("异常: 不是PopupViewBase的视图实例", contentNode.name);
      return;
    }
    this.popupView = popupView;
    this.popupView.setClientData(data);
    this.contentNode = contentNode;
    this.opts = opts;
    this.popupView.popupOpts = this.opts;
    contentNode.parent = this.contentParent;
    contentNode.closeView = this.closeView.bind(this);
  },
  
  onTouchBgMask() {
    if (this.opts && this.opts.closeViewWhenTapMask) {
      this.closeView();
    }
  },
  
  /**
   * 打开视图
   */
  openView() {
    if (this.opts && this.opts.hideBlackMark) {
      this.blackMarkNode.active = false;
    } else {
      this.blackMarkNode.active = true;
    }
    
    if (this.opts && this.opts.onOpen) {
      this.opts.onOpen(this.contentNode);
    }
    let anim;
    if (this.opts) {
      anim = this.opts.anim;
    }
  
    
  },
  
  closeView() {
    let anim;
    if (this.opts) {
      anim = this.opts.anim;
    }
    if (anim === undefined) {
      anim = true
    }
    if (anim) {
      this.anim.stop();
      let clip = this.anim.defaultClip;
      let state = this.anim.getAnimationState(clip.name);
      switch (state.wrapMode) {
        case cc.WrapMode.Reverse:
          state.wrapMode = cc.WrapMode.Default;
          break;
        case cc.WrapMode.Normal:
        case cc.WrapMode.Default:
          state.wrapMode = cc.WrapMode.Reverse;
          break;
        default:
          console.error('未正确反转!')
      }
      this.anim.once('finished', () => {
        if (this.opts && this.opts.onClose) {
          this.opts.onClose(this.contentNode);
        }
        if (this.opts && this.opts.dontDestroy) {
          this.contentNode.removeFromParent(false);
        }
        this.node.destroy();
        popUpViewCount--;
      }, this);
      this.anim.play();
      AudioEngineWrapper.playSound(this.closeAudioClip);
    } else {
      if (this.opts && this.opts.onClose) {
        this.opts.onClose(this.contentNode);
      }
      if (this.opts && this.opts.dontDestroy) {
        this.contentNode.removeFromParent(false);
      }
      this.node.destroy();
      popUpViewCount--;
    }
  }
});


window.functions.openPopupViewWithNode = function (viewNode, data, opts) {
  cc.loader.loadRes(POPUP_VIEW_PREFAB, cc.Prefab, function (err, prefab) {
    let popupViewRoot = cc.instantiate(prefab);
    popupViewRoot.parent = cc.director.getScene();
    let popupView = popupViewRoot.getComponent(PopupViewRoot);
    popupView.setContent(viewNode, data, opts);
    popupView.openView();
  });
};

window.functions.openPopupView = function (prefab, data, opts) {
  let viewNode = cc.instantiate(prefab);
  window.functions.openPopupViewWithNode(viewNode, data, opts);
  return viewNode;
};


window.functions.showPopupViewWithPrefabPath = function (prefabPath, data, opts) {
  Global.Waiting.show();
  cc.loader.loadRes(prefabPath, cc.Prefab, (err, prefab) => {
    Global.Waiting.hide();
    functions.openPopupView(prefab, data, opts);
  });
};


window.functions.getPopupViewCount = function () {
  return popUpViewCount;
};


module.exports = PopupViewRoot;